// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/korbit/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

#include "coin2/base/log.h"

namespace coin2::exchange::korbit::api_order {

void KorbitRestAsyncAsioSubscriber::QueryToken() {
  auto http_context = rest_client()->QueryToken();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&KorbitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KorbitRestAsyncAsioSubscriber::QueryBalance() {
  if (!rest_client()->has_auth_token()) {
    QueryToken();
    return;
  }
  auto http_context = rest_client()->QueryAccountBalance();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&KorbitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KorbitRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_currency_pair(native_symbol);
  params.set_limit("40");
  auto http_context = rest_client()->QueryOpenOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&KorbitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KorbitRestAsyncAsioSubscriber::QueryAllOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_currency_pair(native_symbol);
  params.set_limit("40");
  auto http_context = rest_client()->QueryAllOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&KorbitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KorbitRestAsyncAsioSubscriber::QueryFills(const std::string& native_symbol) {
  RestParams params;
  params.set_currency_pair(native_symbol);
  params.set_limit("40");
  auto http_context = rest_client()->QueryTrades(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&KorbitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KorbitRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(std::bind(&KorbitRestAsyncAsioSubscriber::QueryToken, this), 600, "query_auth");

  AddPeriodicTask(
      std::bind(&KorbitRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance",
      5);

  const auto symbol_list = native_symbols();

  AddPeriodicTask(
      std::bind(&KorbitRestAsyncAsioSubscriber::QueryOpenOrders, this, std::placeholders::_1),
      config_.query_open_orders_period_sec(),
      "query_open_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&KorbitRestAsyncAsioSubscriber::QueryAllOrders, this, std::placeholders::_1),
      config_.query_cancel_orders_period_sec(),
      "query_all_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&KorbitRestAsyncAsioSubscriber::QueryFills, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fills",
      symbol_list);
}

}  // namespace coin2::exchange::korbit::api_order
